package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.wxfootprint.WXFootprintVO;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.service.admin.MarketFootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LC_RSH
 * @date 2022/6/26 11:30
 */
@Service
public class MarketFootprintServiceImpl implements MarketFootprintService {
    @Autowired
    MarketFootprintMapper footprintMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;


    /**
     * 通过userId和goodsId展示收藏
     *
     * @return
     * @author LC_R-SH
     * @since 2022/06/26 16:22
     */
    @Override
    public BaseRespVo query(String userId, String goodsId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        if (!StringUtils.isEmpty(userId)) {
            Integer id1 = 0;
            try {
                id1 = Integer.parseInt(userId.trim());
                criteria.andUserIdEqualTo(id1);
            } catch (Exception e) {
                return BaseRespVo.invalidParameter("参数格式不正确");
            }
        }
        if (!StringUtils.isEmpty(goodsId)) {
            Integer id = 0;
            try {
                id = Integer.parseInt(goodsId.trim());
                criteria.andGoodsIdEqualTo(id);
            } catch (Exception e) {
                return BaseRespVo.invalidParameter("参数格式不正确");
            }
        }
        criteria.andDeletedEqualTo(false);
        marketFootprintExample.setOrderByClause("add_time desc");
        List<MarketFootprint> footprintList = footprintMapper.selectByExample(marketFootprintExample);
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(footprintList);
        CommonData data = CommonData.data(marketFootprintPageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * 微信通过收藏id展示商品
     *
     * @return 商品list
     * @author LC_R-SH
     * @since 2022/06/29 15:46
     */
    @Override
    public BaseRespVo wxQuery(BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        // todo 拼接userId
        // 得到subject
        Subject subject = SecurityUtils.getSubject();
        // 得到当前用户对象
        MarketUser currentUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        // 得到userId
        Integer userId = currentUser.getId();
        // Integer userId = 1;
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria footCriteria = footprintExample.createCriteria();
        footCriteria.andUserIdEqualTo(userId);
        footCriteria.andDeletedEqualTo(false);
        footprintExample.setOrderByClause("update_time desc");
        List<MarketFootprint> footprintList = footprintMapper.selectByExample(footprintExample);

        // 新建 WXFootprintVO list
        List<WXFootprintVO> wxFootprintVOS = new ArrayList();

        for (MarketFootprint footprint : footprintList) {
            MarketGoods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            WXFootprintVO vo = new WXFootprintVO();
            vo.setBrief(goods.getBrief());
            vo.setPicUrl(goods.getPicUrl());
            // 历史足迹添加时间应为更新时间/伪删除
            vo.setAddTime(footprint.getUpdateTime());
            vo.setGoodsId(goods.getId());
            vo.setName(goods.getName());
            vo.setId(footprint.getId());
            vo.setRetailPrice(goods.getRetailPrice());
            wxFootprintVOS.add(vo);
        }
        PageInfo<WXFootprintVO> marketGoodsPageInfo = new PageInfo<>(wxFootprintVOS);
        CommonData data = CommonData.data(marketGoodsPageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * 删除历史足迹
     *
     * @return
     * @author LC_R-SH
     * @since 2022/06/29 16:18
     */
    @Override
    public BaseRespVo delete(MarketGoods goods) {
        MarketFootprint footprint = new MarketFootprint();
        footprint.setId(goods.getId());
        footprint.setDeleted(true);
        footprint.setUpdateTime(new Date());
        int update = footprintMapper.updateByPrimaryKeySelective(footprint);
        if (update == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData("删除失败");
    }

    /**
     * 查看商品时增加历史足迹
     *
     * @return
     * @author LC_R-SH
     * @since 2022/06/29 16:27
     */
    @Override
    public void insert(Integer goodsId) {
        //
        Integer userId = 0;
        //todo 得到subject
        Subject subject = SecurityUtils.getSubject();
        // 得到当前用户对象
        PrincipalCollection principals = subject.getPrincipals();
        // 判断是否有登录信息
        if (!ObjectUtils.isEmpty(principals)) {
            MarketUser currentUser = (MarketUser) principals.getPrimaryPrincipal();
            // 得到userId
            userId = currentUser.getId();
        }
        // Integer userId = 1;
        // 查找历史足迹是否存在 存在则更新时间
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andGoodsIdEqualTo(goodsId);
        List<MarketFootprint> footprintList = footprintMapper.selectByExample(footprintExample);
        // 存在则更新时间
        if (!ObjectUtils.isEmpty(footprintList)) {
            MarketFootprint footprint = new MarketFootprint();
            footprint.setUpdateTime(new Date());
            footprint.setAddTime(new Date());
            footprint.setDeleted(false);
            footprintMapper.updateByExampleSelective(footprint, footprintExample);
        } else {
            // 不存在则新增历史记录
            MarketFootprint footprint = new MarketFootprint();
            footprint.setUserId(userId);
            footprint.setGoodsId(goodsId);
            footprint.setAddTime(new Date());
            footprint.setUpdateTime(new Date());
            footprint.setDeleted(false);
            footprintMapper.insertSelective(footprint);
        }
    }
}

/**
 * 会员收藏
 *
 * @author 86157
 * @since 2022/06/26 11:30
 */